<!DOCTYPE HTML>
<%@ page import="languageguide.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %><jsp:useBean id="readingManager" scope="application" class="org.languageguide.readings.ReadingManager" /><html>
<head>
<%
	response.setHeader("Cache-Control","no-cache"); 
	response.setHeader("Pragma","no-cache"); 
	response.setDateHeader ("Expires", -1); 
	org.languageguide.user.User user = org.languageguide.user.UserManager.getUser(session);
	String readingIdStr = request.getParameter("id");
    Integer readingId = Integer.parseInt(readingIdStr);
    //String numberOfRows = "10";
    String target = request.getParameter("target");
	java.util.Collection<org.languageguide.readings.SentenceTranslation> sentenceTranslations =  readingManager.getSentenceTranslationList(readingId, target); 
	Integer numberOfRows =  sentenceTranslations.size();
	
	
	java.util.List<String> translations =  readingManager.getTranslationsFromSession(session);
	
String redirectField      = "";
String redirectLocation     = "/user/readings/translate_reading_session_translations.jsp?";
  
%>
<%@ include file="/user/load_user_with_redirect_specifiy_url.jsp" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function up(value){
	var previousElem  =  document.getElementById(value - 1);
	if (previousElem.value != ""){
		alert("the text area above this one isn't empy");
		return
	}
	var elem =  document.getElementById(value++);
	while(elem != null){
		previousElem.value = elem.value;
		previousElem = elem;
		elem =  document.getElementById(value++);
	}

	//while (elem != null){
	
	//}
}

function down(value){
		//find the next blank text area
	var currentElem  =  document.getElementById(value++);
	var savedTranslation = currentElem.value;
	currentElem.value = "";
	var nextElem =  document.getElementById(value++);
	while (nextElem.value !== ""){
		var nextSaved = nextElem.value;
		nextElem.value = savedTranslation;
		savedTranslation = nextSaved;
		nextElem =  document.getElementById(value++);
	}
	nextElem.value = savedTranslation;
}
</script>
</head>

<body>
<form name="form1" method="post" onSubmit="return allowSubmit()" action="/user/readings/translate_reading_save.jsp">
  <table width="100%" border="0" cellpadding="3" cellspacing="1" align="center">

 
<% 
int order = 0;
int lastParagraph = 0;
for (org.languageguide.readings.SentenceTranslation translation: sentenceTranslations){ 
order++;
int paragraph = translation.getParagraph();
if (paragraph != lastParagraph){
lastParagraph = paragraph;
%>
<tr><td><button type="button" onclick="translationParagraph(<%= paragraph %>)">Google Translate Paragraph!</button></td></tr>
<%  }%>
			<tr><td><%= translation.getSentence() %></td><td>&nbsp;</td></tr>
			<tr><td>
            <% if (translations.size() > order - 1){%>
				<textarea rows="3" cols="120" id="<%= order %>" name="<%= order %>"><%=translations.get(order - 1)%></textarea>			
			<%}else{%>	
            	<textarea rows="3" cols="120" id="<%= order %>" name="<%= order %>"></textarea>
			<%}%>	
			</td><td><button onclick="up(<%= order %>)" type="button">Up</button> <button onclick="down(<%= order %>)" type="button">Down</button></td></tr>
<% } %>
 <%  if (translations.size() > order){  
	 	for (int i = order; i < translations.size(); i++){ %>
 				<tr><td><textarea rows="3" cols="120" id="<%= i %>" name="<%= i %>"><%=translations.get(i)%></textarea></td></tr>
	<%	} 
  	} else{ %>
			<tr><td><textarea rows="3" cols="120" id="<%= (order + 1) %>" ></textarea></td></tr>
            <tr><td><textarea rows="3" cols="120" id="<%= (order + 2) %>"></textarea></td></tr>
            <tr><td><textarea rows="3" cols="120" id="<%= (order + 3) %>"></textarea></td></tr>
		<%	}
 %>
 
  </table><br>
<div align="center">
  <input type="submit" name="translation.submit" id="saveButton" value="Save" style="font-weight:bold; font-size: 120%">   <input type="submit" name="translation.submit"  value="Cancel" id="cancelButton" style="font-weight:bold; font-size: 120%; margin-left 20px;">
  </div>
  
  <input name="readingId" type="hidden" value="<%= readingId %>">
  <input name="target" type="hidden" value="<%= target %>">
    <input name="row_count" type="hidden" value="<%= numberOfRows %>">
 <%=  redirectField %>
 
</form>
</body>
</html>
